<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Promise链式调用 catch</title>
</head>
<body>
<p>promise异常具有冒泡性质，会一直向后传递，直到被捕获为止</p>
<p>如果没有指定catch错误处理的回调函数，promise对象抛出的错误不会传递到外层代码，即不会有任何反应</p>
<script>
  const getJson = function (url) {
    const promise = new Promise((resolve, reject) => {
      const handler = function () {
        if (this.readyState !== 4) {
          return;
        }
        if (this.status == 200) {
          resolve(this.response);
        } else {
          reject(new Error(this.statusText));
        }
      };
      const client = new XMLHttpRequest();
      client.open("GET", url);
      client.onreadystatechange = handler;
      client.responseType = "json";
      client.setRequestHeader("Accept", "application/json");
      client.send();
    });
    return promise;
  };

  getJson("/post/1.json").then(post => {
    getJson(post.url);
  }).then(comments => {
    console.log(comments);
  }).catch(err => {
    console.log(err);
  });

  const doSomething = function () {
    return new Promise((resolve, reject) => {
      resolve(x + 2);
    });
  };

  doSomething().then(() => {
    console.log("hello");
  });

  setTimeout(() => {
    console.log("123");
  }, 1000)
</script>
</body>
</html>